package com.googlecode.compliancechecker;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashSet;
import java.util.Set;

import org.objectweb.asm.AnnotationVisitor;
import org.objectweb.asm.Attribute;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.FieldVisitor;
import org.objectweb.asm.MethodVisitor;

/**
 * Utility class to collect all parent types (classes and interfaces) of a
 * certain type.
 * 
 * @author Marc-Christian Schulze
 * 
 */
public class ParentTypeCollector implements ClassVisitor {

	private Set<String>	parentTypes;

	public Set<String> collect(InputStream stream) throws IOException {
		parentTypes = new HashSet<String>();
		ClassReader cr = new ClassReader(stream);
		cr.accept(this, 0);
		return parentTypes;
	}

	public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
		if (superName != null) {
			parentTypes.add(superName);
		}
		if (interfaces != null) {
			for (String ifc : interfaces) {
				if (ifc != null) {
					parentTypes.add(ifc);
				}
			}
		}
	}

	public AnnotationVisitor visitAnnotation(String arg0, boolean arg1) {
		return null;
	}

	public void visitAttribute(Attribute arg0) {
	}

	public void visitEnd() {
	}

	public FieldVisitor visitField(int arg0, String arg1, String arg2, String arg3, Object arg4) {
		return null;
	}

	public void visitInnerClass(String arg0, String arg1, String arg2, int arg3) {
	}

	public MethodVisitor visitMethod(int arg0, String arg1, String arg2, String arg3, String[] arg4) {
		return null;
	}

	public void visitOuterClass(String arg0, String arg1, String arg2) {
	}

	public void visitSource(String arg0, String arg1) {
	}
}
